<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>台球运动</title>
    <style>
        canvas{
            border: 1px solid #333;
        }
    </style>
    
</head>
<body>
    <canvas id="canvas" width="600" height="400"></canvas>

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');

        let x = 0;
        let y = 0;
        let xd = true;
        let yd = true;
        ctx.fillStyle = 'red';
        ctx.fillRect(x,y,50,50);

        setInterval(() => {
            ctx.clearRect(x,y,50,50);
            xd? x++ : x--;
            yd? y++ : y--;
            if(x===550){
                xd = false;
            }
            if(x === 0){
                xd = true
            }

            if(y === 350){
                yd = false;
            }
            if(y === 0){
                yd = true;
            }
            ctx.fillRect(x,y,50,50);
        },10);
    </script>
</body>
</html>